package com.xutong.myfunction.custom;

import android.content.Context;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by Administrator on 2017/2/7.
 */
public class SuspensionButton extends Button {

    private boolean moveable = true;
    private float currentX,currentY,currentLeft,currentTop;
    private ViewGroup.MarginLayoutParams mlp;
    long time=0;
    private float viewWidth,viewHeight,parentWidth,parentHeight;
    private float minLeftMargin,maxLeftMargin,minTopMargin,maxTopMargin;
    private float leftPadding,rightDistance,topPadding,bottomDistance;
    public SuspensionButton(Context context) {
        super(context);
    }

    public SuspensionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SuspensionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        if (moveable){
            ViewGroup parentView = (ViewGroup) getParent();
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) getLayoutParams();
            viewWidth = getRight() - getLeft();
            viewHeight = getBottom() - getTop();
            parentWidth = parentView.getMeasuredWidth();
            parentHeight = parentView.getMeasuredHeight();
            minLeftMargin = lp.leftMargin;
            leftPadding = parentView.getPaddingLeft();
            rightDistance = lp.rightMargin + parentView.getPaddingRight();
            maxLeftMargin = parentWidth - rightDistance - viewWidth - leftPadding;
            minTopMargin = lp.topMargin;
            topPadding = parentView.getPaddingTop();
            bottomDistance = lp.bottomMargin + parentView.getPaddingBottom();
            maxTopMargin = parentHeight - bottomDistance - viewHeight - topPadding;

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                time = System.currentTimeMillis();
//                Log.e("xutong","ACTION_DOWN,time="+time);
                if (moveable){
                     mlp = (ViewGroup.MarginLayoutParams) getLayoutParams();
                    currentX = event.getRawX();
                    currentY = event.getRawY();
                    currentLeft = mlp.leftMargin;
                    currentTop = mlp.topMargin;
                }

                break;
            case MotionEvent.ACTION_MOVE:
//                Log.e("xutong","ACTION_MOVE");
                if (moveable){
                    currentLeft += event.getRawX()-currentX;
                    currentTop += event.getRawY() -currentY;

                    currentLeft = currentLeft < minLeftMargin ? minLeftMargin : currentLeft;
                    currentLeft = (leftPadding+currentLeft+viewWidth+rightDistance) > parentWidth ? maxLeftMargin : currentLeft;

                    currentTop = currentTop < minTopMargin ? minTopMargin : currentTop;
                    currentTop = (topPadding+currentTop+viewHeight+bottomDistance) > parentHeight ? maxTopMargin : currentTop;

                    mlp.leftMargin = (int) currentLeft;
                    mlp.topMargin = (int) currentTop;
                    setLayoutParams(mlp);
                    currentX = event.getRawX();
                    currentY = event.getRawY();
                }

                break;
            case MotionEvent.ACTION_UP:

                long time2 = System.currentTimeMillis();
//                Log.e("xutong","ACTION_UP,time2="+time2);
                if (time2-time>200){
                    moveable=false;
                }

                break;
        }
//        Log.e("xutong","moveable="+moveable);
        if (moveable){
            return super.onTouchEvent(event);
        }else {
            moveable = true;
            return true;

        }

    }

}
